<template>
  <div>
    <h3>注册页面</h3>
    <el-form :model="ruleUser" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm" style="width:600px;margin:0 auto">
        <el-form-item label="用户名" prop="username">
            <el-input  v-model="ruleUser.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="ruleUser.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="password2">
            <el-input type="password" v-model="ruleUser.password2" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
            <el-input  v-model="ruleUser.mobile" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="图形验证码" prop="imagecode">
            <el-input  v-model="ruleUser.imagecode" autocomplete="off"></el-input>
            <div>
                <img :src="imageUrl" alt="图形验证码" @click="getImage">
            </div>
        </el-form-item>
        
        <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
            <!-- <el-button @click="resetForm('ruleForm')">重置</el-button> -->
        </el-form-item>
</el-form>
  </div>
</template>

<script>
import axios from 'axios'
import {v4 as uuid} from 'uuid'
  export default {
    data() {
      var validateUsername = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('用户名不能为空'));
        }
        const userRules=/^[a-zA-Z][0-9A-Za-z]{4,16}$/
        if(userRules.test(value)){
            axios.get("http://127.0.0.1:8000/users/checkname/"+value+'/')
            .then(resp=>{
                console.log(resp);
                if(resp.data.code == 200){
                    return callback()
                }else{
                    return callback(resp.data.msg);
                }
            })
        }else{
           return callback(new Error('用户名不符合规则'));
        }
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
           callback(new Error('请输入密码'));
        } 
        const passRules=/^[a-zA-Z][0-9a-zA-Z]{4,16}$/
        if(passRules.test(value)){
           return callback()
        }else{
             return callback(new Error('密码不符合规则'));
        }
        
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleUser.password) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };

      var validateMobile = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('手机号不能为空'));
        } 
        const userRules=/^1[3-9][0-9]{4,16}$/
        if(userRules.test(value)){
            axios.get("http://127.0.0.1:8000/users/checkmobile/"+value+'/')
            .then(resp=>{
                console.log(resp);
                if(resp.data.code == 200){
                    return callback()
                }else{
                    return callback(resp.data.msg);
                }
            })
        }else{
           return callback(new Error('手机号不符合规则'));
        }

      };

      var validateImage = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('验证码不可为空'));
        } 
        const imageRules=/^[a-zA-Z0-9]{4}$/
        axios.get('http://127.0.0.1:8000/users/checkimage/'+this.imageID+'/',{code:value})
        .then(resp=>{
            if(resp.data.msg ==200){
                return callback()
            }else{
                return callback(resp.data.msg);
            }
        })
        .catch(err=>{
            console.log(err);
        })
      };
      return {
            imageID:'',
            imageUrl:'',
        ruleUser: {
            username:'',
          password: '',
          password2: '',
          mobile: '',
          imagecode: '',
        },
        rules: {
          username:[
            { validator: validateUsername, trigger: 'blur' }
          ],
          password: [
            { validator: validatePass, trigger: 'blur' }
          ],
          password2: [
            { validator: validatePass2, trigger: 'blur' }
          ],

          mobile: [
            { validator: validateMobile, trigger: 'blur' }
          ],
          imagecode: [
            { validator:validateImage , trigger: 'blur' }
          ]
        }
      };
    },
    mounted(){
        this.getImage()
    },
    methods: {
      getImage(){
        this.imageID=uuid()
        this.imageUrl='http://127.0.0.1:8000/users/checkimage/'+this.imageID+'/'
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            axios.post('http://127.0.0.1:8000/users/reg/',this.ruleUser)
            .then(resp=>{
                console.log(resp);
                alert('注册成功')
                
                this.$router.push('/login')
            })
            .catch(err=>{
                console.log(err);
            })
            
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<style>

</style>